home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8375 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.6 KB

  1. Path: qns2.qns.com!not-for-mail
  2. From: mjarvis@qns2.qns.com (Michael Jarvis)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Hiding a password
  5. Date: 3 Mar 1996 14:12:11 -0600
  6. Organization: Questar Network Services
  7. Message-ID: <4hcuer$2q5@qns2.qns.com>
  8. References: <1996Feb29.224936.137160@forest>
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. ebromber@forest.drew.edu (ebromber@forest.drew.edu) wrote:
  12. > I recently wrote a password program. However, there is one little flaw I 
  13. > want to fix. When the password is entered, it is visible on the screen. I 
  14. > was wondering if anyone knows how to hide the password by  printing 
  15. > asterisks instead of the actual character. 
  16.  
  17. From the comp.lang.c FAQ, available at 
  18.  
  19. ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list
  20.  
  21. 19.1:    How can I read a single character from the keyboard without
  22.     waiting for the RETURN key?  How can I stop characters from
  23.     being echoed on the screen as they're typed?
  24.  
  25. A:    Alas, there is no standard or portable way to do these things in
  26.     C.  Concepts such as screens and keyboards are not even
  27.     mentioned in the Standard, which deals only with simple I/O
  28.     "streams" of characters.
  29.  
  30.     At some level, interactive keyboard input is usually collected
  31.     and presented to the requesting program a line at a time.  This
  32.     gives the operating system a chance to support input line
  33.     editing (backspace/delete/rubout, etc.) in a consistent way,
  34.     without requiring that it be built into every program.  Only
  35.     when the user is satisfied and presses the RETURN key (or
  36.     equivalent) is the line made available to the calling program.
  37.     Even if the calling program appears to be reading input a
  38.     character at a time (with getchar() or the like), the first call
  39.     blocks until the user has typed an entire line, at which point
  40.     potentially many characters become available and many character
  41.     requests (e.g. getchar() calls) are satisfied in quick
  42.     succession.
  43.  
  44.     When a program wants to read each character immediately as it
  45.     arrives, its course of action will depend on where in the input
  46.     stream the line collection is happening and how it can be
  47.     disabled.  Under some systems (e.g. MS-DOS, VMS in some modes),
  48.     a program can use a different or modified set of OS-level input
  49.     calls to bypass line-at-a-time input processing.  Under other
  50.     systems (e.g. Unix, VMS in other modes), the part of the
  51.     operating system responsible for serial input (often called the
  52.     "terminal driver") must be placed in a mode which turns off line-
  53.     at-a-time processing, after which all calls to the usual input
  54.     routines (e.g. read(), getchar(), etc.) will return characters
  55.     immediately.  Finally, a few systems (particularly older, batch-
  56.     oriented mainframes) perform input processing in peripheral
  57.     processors which cannot be told to do anything other than line-
  58.     at-a-time input.
  59.  
  60.     Therefore, when you need to do character-at-a-time input (or
  61.     disable keyboard echo, which is an analogous problem), you will
  62.     have to use a technique specific to the system you're using,
  63.     assuming it provides one.  Since comp.lang.c is oriented towards
  64.     topics that C does deal with, you will usually get better
  65.     answers to these questions by referring to a system-specific
  66.     newsgroup such as comp.unix.questions or
  67.     comp.os.msdos.programmer, and to the FAQ lists for these groups.
  68.     Note that the answers are often not unique even across different
  69.     variants of a system; bear in mind when answering system-
  70.     specific questions that the answer that applies to your system
  71.     may not apply to everyone else's.
  72.  
  73.     However, since these questions are frequently asked here, here
  74.     are brief answers for some common situations.
  75.  
  76.     Some versions of curses have functions called cbreak(),
  77.     noecho(), and getch() which do what you want.  If you're
  78.     specifically trying to read a short password without echo, you
  79.     might try getpass().  Under Unix, you can use ioctl() to play
  80.     with the terminal driver modes (CBREAK or RAW under "classic"
  81.     versions; ICANON, c_cc[VMIN] and c_cc[VTIME] under System V or
  82.     POSIX systems; ECHO under all versions), or in a pinch, system()
  83.     and the stty command.  (For more information, see <sgtty.h> and
  84.     tty(4) under classic versions, <termio.h> and termio(4) under
  85.     System V, or <termios.h> and termios(4) under POSIX.)  Under MS-
  86.     DOS, use getch() or getche(), or the corresponding BIOS
  87.     interrupts.  Under VMS, try the Screen Management (SMG$)
  88.     routines, or curses, or issue low-level $QIO's with the
  89.     IO$_READVBLK function code (and perhaps IO$M_NOECHO, and others)
  90.     to ask for one character at a time.  (It's also possible to set
  91.     character-at-a-time or "pass through" modes in the VMS terminal
  92.     driver.)  Under other operating systems, you're on your own.
  93.  
  94.     (As an aside, note that simply using setbuf() or setvbuf() to
  95.     set stdin to unbuffered will *not* generally serve to allow
  96.     character-at-a-time input.)
  97.  
  98.     If you're trying to write a portable program, a good approach is
  99.     to define your own suite of three functions to (1) set the
  100.     terminal driver or input system into character-at-a-time mode
  101.     (if necessary), (2) get characters, and (3) return the terminal
  102.     driver to its initial state when the program is finished.
  103.     (Ideally, such a set of functions might be part of the C
  104.     Standard, some day.)  The extended versions of this FAQ list
  105.     (see question 20.40) contain examples of such functions for
  106.     several popular systems.
  107.  
  108.     See also question 19.2.
  109.  
  110.     References: PCS Sec. 10 pp. 128-9, Sec. 10.1 pp. 130-1; POSIX
  111.     Sec. 7.
  112.  
  113. -- 
  114. Michael Jarvis   |  Finger for PGP Public key  |   QNSnet Technical Support
  115. mjarvis@qns.com  | http://www.qns.com/~mjarvis |   Questar Network Services
  116. GC3.1: GCS d s+++: a26 C++++ USLV++++$ P++++ L++ E--- W++ N++ !o K+ W-- !O 
  117. M- !V PS+ PE Y+ PGP+ t+ 5 X R tv b+++ DI+++ D++ G+ e>++ h---(*) r+++ y+++
  118.